<template>
	<view>
		<uni-nav-bar left-icon="left" :statusBar="true"   titleSize="30px" leftText="返回"     title="选择客户"  @clickLeft="back"   :fixed="true" backgroundColor="#3286ed" color="#fff"/>
	     <!-- 搜索框 -->
		 <u-search  style="position:fixed;z-index:20;width:100%;height;40px;background-color: #fff;padding: 10px;" @clear="clearFn" :clearabled="true"
		  showAction @custom="customFn" @search="inputSearchBtn" v-model.enter="queryParams.name"  height='30' placeholder="请输入姓名" shape="square" actionText='搜索'></u-search>
	     <view style="height:50px;width: 100%;display: block;"></view>
	     <!--列表展示  -->
		 <scroll-view scroll-y="true" lower-threshold="500px" @scrolltolower="lower">
				 <view>
					 <view class="box" v-for="(item,index) in customerList" :key="index">
						 <view  >
							<view>姓名：{{item.name}}</view>
							<view>联系人：{{item.contactName}}</view>
							<!-- <view>联系人电话：{{item.contactTelephone}}</view> -->
							 <view>负责人：{{item.chargePersonName}}</view>
							<view>标签：{{item.label}}</view>
							<view>所属区域：{{item.region}}</view>
							<view>类别：{{item.categoryName}}</view> 
							<view class="title" @click="checked(item)"><u-button type="success" size="mini" style="width: 60px;height: 25px;" >选择</u-button></view>
						 </view>
					 	
					 </view>
				 </view>
		 </scroll-view>
	</view>
</template>

<script>
	import {listCustomer} from '@/api/customer.js'
	import {getDicts} from '@/api/dictionary/index.js'
	export default {
		data(){
			return{
				// 查询参数
				 queryParams: {
				        pageNum: 1,
				        pageSize: 10,
				        name: null,
				        contactName: null,
				        contactTelephone: null,
				        label: null,
				        source: null,
				        region: null,
				        category: null,
				        address: null,
				        createId: null,
				        lossReasons: null,
				        lossRemark: null,
				        chargePersonId: null,
				        chargePersonName: null,
				        collaboratorId: null,
				        collaboratorName: null,
				        finalFollowTime: null,
				        allocationTime: null,
				        status: null,
				        customerStatus: null,
				        search:null
				      },
					  customerList:[],
					  total:0,
					  customer:{},
					  labelOptions:[],
					  categoryOptions:[]
				
			}
		},
		onLoad(){
			this.getList()
			this.getlabeloptions()
			this.getcategoryOptions()
		},
		// 上划
		onReachBottom() {
			if(this.queryParams.pageSize*this.queryParams.pageNum<this.total){
				console.log(this.total);
						 this.queryParams.pageNum++
						 let res=listCustomer(this.queryParams).then(reponse=>{
							        console.log(reponse);
									// 添加新属性
									let categoryNameObject={categoryName:''}
									 reponse.rows.forEach(item=>{
									let newItem=Object.assign(item,categoryNameObject)
										 newItem.label=this.getValue(this.labelOptions,item.label)
										 newItem.categoryName=this.getValue(this.categoryOptions,item.category)
									 })
						 			 this.customerList.push(...reponse.rows)
									  console.log(this.listCustomer);
						 })
						
						 
			}else{
				console.log(this.total);
						 console.log('已经加载完毕');
			}
		},
		methods:{
			back(){
				uni.navigateBack({
					delta:1
				})
			},
			async getList(){
				// 查询我负责的客户
				this.queryParams.search=2;
				let res=await listCustomer(this.queryParams)
				this.total=res.total
				// 添加新属性
				let categoryNameObject={categoryName:''}
				 res.rows.forEach(item=>{
				let newItem=Object.assign(item,categoryNameObject)
					 newItem.label=this.getValue(this.labelOptions,item.label)
					 newItem.categoryName=this.getValue(this.categoryOptions,item.category)
				 })
				 // console.log(res.rows);
				 this.customerList=res.rows
				 // console.log(this.customerList);
			},
			getValue(arr, item) {
			      for (let i = 0; i < arr.length; i++) {
			        if (item == arr[i].dictValue) {
			          return arr[i].dictLabel
			        }
			      }
			    },
				async getlabeloptions(){
					 let res= await getDicts('biz_label')
					 
					 this.labelOptions=res.data
				},
				async getcategoryOptions(){
					let res=await getDicts('biz_category')
					this.categoryOptions=res.data
				},
				// 上划加载
				lower(e){
					console.log(e);
				},
			clearFn(){},
			customFn(){},
			inputSearchBtn(){},
			// 选中
			checked(e){
				 
				this.customer=e
				console.log(this.customer);
				// 1. 获取当前页面栈实例（此时最后一个元素为当前页）
				let pages = getCurrentPages() 
					console.log(pages);
					// 2. 上一页面实例
					// 注意是length长度，所以要想得到上一页面的实例需要 -2
					// 若要返回上上页面的实例就 -3，以此类推
				let prevPage = pages[pages.length -2] 
				console.log(prevPage);
				// 3. 给上一页面实例绑定getValue()方法和参数（注意是$vm）
				   prevPage.$vm.getValueS(this.customer) 
				// 4. 返回上一页面
				 uni.navigateBack({
					 delta: 1 // 返回的页面数
				 })
			}
		},
	}
</script>

<style lang="scss">
	.box{
		position: relative;
		display: block;
		width: 95%;
		min-height:100px;
		margin: 10px ;
		padding: 10px;
		font-size: 12px;
		background-color: #fff;
		border: solid 1px #c3c3c3;
		.title{
			position: absolute;
			right: 20px;
			bottom: 30px;
			color: #fd9540;
			font-size: 14px;
		}
		
	}
	
</style>